import 'package:flutter/material.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class QGTypeSwitchWidget extends StatefulWidget {
  int current;
  List<String> items;
  MainAxisAlignment alignment;
  Function(int index) onSwitch;
  bool subline;
  bool underline;

  double fontSize;
  double maxFontSize;

  double paddingLen;

  QGTypeSwitchWidget(
      {Key? key,
      required this.current,
      required this.items,
      required this.onSwitch,
      this.fontSize = 14,
      this.maxFontSize = 14,
      this.subline = false,
      this.underline = false,
      this.paddingLen = 10,
      this.alignment = MainAxisAlignment.spaceBetween})
      : super(key: key);

  @override
  State<QGTypeSwitchWidget> createState() => _QGTypeSwitchWidgetState();
}

class _QGTypeSwitchWidgetState extends State<QGTypeSwitchWidget> {
  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];
    for (int i = 0; i < widget.items.length; i++) {
      var child = GestureDetector(
        onTap: () {
          setState(() {});
          widget.onSwitch(i);
        },
        child: Stack(
          children: [
            Visibility(
              visible: widget.underline && i == widget.current,
              child: Positioned(
                  bottom: 7.rpx,
                  left: 0,
                  right: 0,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [

                       QGImage(
                            asset: "xhuanzhaonf", width: 34.rpx
                          ),

                      // Container(
                      //   decoration: BoxDecoration(
                      //       borderRadius: BorderRadius.only(
                      //           topLeft: Radius.circular(50.rpx),
                      //           topRight: Radius.circular(50.rpx)),
                      //       color: const Color.fromARGB(255, 255, 0, 0)),
                      //   width: 34.rpx,
                      //   height: 4.rpx,
                      // ),
                    ],
                  )),
            ),
            Container(
              color: Colors.transparent,
              padding: EdgeInsets.symmetric(
                  horizontal: widget.paddingLen.rpx, vertical: 12.rpx),
              child: Stack(
                children: [
                  Visibility(
                    visible: widget.subline && i == widget.current,
                    child: Positioned(
                      left: 0,
                      right: 0,
                      bottom: 2.rpx,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Container(
                            width: 34.rpx,
                            height: 4.rpx,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(50.rpx),
                                    topRight: Radius.circular(50.rpx)),
                                color: const Color.fromARGB(255, 66, 46, 46)),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Text(
                    widget.items[i],
                    style: TextStyle(
                        color: (i == widget.current)
                            ? const Color(0xFFffffff)
                            : const Color(0xFF989EA4),
                        fontSize: (i == widget.current)
                            ? toFit(widget.maxFontSize)
                            : toFit(widget.fontSize),
                        fontWeight: (i == widget.current)
                            ? FontWeight.bold
                            : FontWeight.normal),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
      children.add(child);
    }

    return Row(mainAxisAlignment: widget.alignment, children: children);
  }
}
